package com.sctvcloud.renhe.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;


import com.ruihang.generalibrary.utils.GeneraLifeObser;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.ui.activities.LoginActivity;
import com.sctvcloud.renhe.ui.utils.UIUtils;
import com.sctvcloud.renhe.ui.widget.MyFragmentTabHost;
import com.sctvcloud.renhe.utils.UserManager;

import java.util.ArrayList;

public abstract class BaseTopTabFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener {

    private MyFragmentTabHost tabHost;
//    private ImageView centerImage;
//    private ImageView centerText;
//    private LinearLayout centerLayout;

    private int textSelectedColor;

    private ArrayList<String> tabsTxt;
    private ArrayList<Boolean> tabClip;

    private ArrayList<Integer> tabsNormalImg;

    private ArrayList<Integer> tabsSelectImg;

    private ArrayList<Class> tabsFragments;
    private View.OnClickListener centerClickListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_CREATED);
        setContentView(R.layout.base_tab_activity_manager);
        tabHost = (MyFragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager()
                , R.id.ac_contentLayout);
        tabHost.getTabWidget().setDividerDrawable(null);
        tabHost.setOnTabChangedListener(this);
//        centerLayout = (LinearLayout) findViewById(R.id.center_layout);
//        centerLayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (null != centerClickListener) {
//                    centerClickListener.onClick(v);
//                }
//            }
//        });
//        centerImage = (ImageView) findViewById(R.id.main_image_center);
//        centerText = (ImageView) findViewById(R.id.main_tv_final);
        tabsTxt = new ArrayList<>();
        tabsNormalImg = new ArrayList<>();
        tabsSelectImg = new ArrayList<>();
        tabsFragments = new ArrayList<>();
        tabClip = new ArrayList<>();

    }

    public String getOwnerName() {
        return this + "";
    }

    @Override
    public void onStart() {
        super.onStart();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_START);
    }

    @Override
    protected void onResume() {
        super.onResume();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_RESUME);

    }

    @Override
    protected void onPause() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_PAUSE);
        super.onPause();
    }


    @Override
    public void onStop() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_STOP);
        super.onStop();
    }


    @Override
    protected void onDestroy() {
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_DESTORY);
        GeneraLifeObser.removeLife(this);
        super.onDestroy();
    }

    public void addTab(String text, Integer normalImg, Integer selectImg, Class fragmentClass, boolean isClip) {
        tabsTxt.add(text);
        if (null != normalImg) {
            tabsNormalImg.add(normalImg);
            tabsSelectImg.add(selectImg);
        }
        tabsFragments.add(fragmentClass);
        tabClip.add(isClip);
    }

    public void commit(int textSelectColor) {
        this.textSelectedColor = textSelectColor;
        tabHost.clearAllTabs();
        initTab();
    }


    private void initTab() {
        if (null != tabsTxt && tabsTxt.size() > 0) {
            for (int i = 0; i < tabsTxt.size(); i++) {
                TabHost.TabSpec tabSpec = tabHost.newTabSpec(tabsTxt.get(i)).setIndicator(getTabView(i));
                tabHost.addTab(tabSpec, tabsFragments.get(i), null);
                tabHost.setTag(i);
            }
            TabWidget tabWidget = tabHost.getTabWidget();
            for (int i = 0; i < tabWidget.getChildCount(); i++) {
                tabWidget.getChildAt(i).getLayoutParams().height = UIUtils.dip2px(this, 48);
            }
        }

    }

    public abstract void onTabChanged(int index, String title);

    private View getTabView(int idx) {
        View view = LayoutInflater.from(this).inflate(R.layout.footer_tabs, null);
        if (!tabClip.get(idx)) {
//            centerLayout.setVisibility(View.VISIBLE);
//            ((TextView) view.findViewById(R.id.tvTab)).setVisibility(View.GONE);
//            ((ImageView) view.findViewById(R.id.ivImg)).setVisibility(View.GONE);
//            centerImage.setImageResource(tabsSelectImg.get(idx));
//            centerText.setText(tabsTxt.get(idx));
        } else {
            if (TextUtils.isEmpty(tabsTxt.get(idx))) {
                ((TextView) view.findViewById(R.id.tvTab)).setVisibility(View.GONE);
            } else {
                ((TextView) view.findViewById(R.id.tvTab)).setVisibility(View.VISIBLE);
                ((TextView) view.findViewById(R.id.tvTab)).setText(tabsTxt.get(idx));
            }
            if (idx == 0) {
                ((TextView) view.findViewById(R.id.tvTab)).setTextColor(this.getResources().getColor(textSelectedColor));
                if (null != tabsSelectImg && tabsSelectImg.size() > idx) {
                    ((ImageView) view.findViewById(R.id.ivImg)).setVisibility(View.VISIBLE);
                    ((ImageView) view.findViewById(R.id.ivImg)).setImageResource(tabsSelectImg.get(idx));
                } else {
                    ((ImageView) view.findViewById(R.id.ivImg)).setVisibility(View.GONE);
                }
            } else {
                if (null != tabsNormalImg && tabsNormalImg.size() > idx) {
                    ((ImageView) view.findViewById(R.id.ivImg)).setVisibility(View.VISIBLE);
                    ((ImageView) view.findViewById(R.id.ivImg)).setImageResource(tabsNormalImg.get(idx));
                } else {
                    ((ImageView) view.findViewById(R.id.ivImg)).setVisibility(View.GONE);
                }
            }
        }
        return view;
    }


    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub
        updateTab();
    }

    public void setCurrentTab(int index) {
        tabHost.setCurrentTab(index);
    }

    private void updateTab() {
        TabWidget tabw = tabHost.getTabWidget();
        boolean isSelectCenter = TextUtils.isEmpty(tabsTxt.get(tabHost.getCurrentTab()));
        for (int i = 0; i < tabw.getChildCount(); i++) {
            View view = tabw.getChildAt(i);
            ImageView iv = (ImageView) view.findViewById(R.id.ivImg);
            if (tabClip.get(i)) {
                if (i == tabHost.getCurrentTab()) {
                    onTabChanged(i, tabsTxt.get(i));
                    if (!isSelectCenter) {
                        ((TextView) view.findViewById(R.id.tvTab)).setTextColor(this.getResources().getColor(textSelectedColor));
                        if (null != tabsSelectImg && tabsSelectImg.size() > i) {
                            iv.setImageResource(tabsSelectImg.get(i));
                        }
                    }
                } else {
                    if (!isSelectCenter) {
                        ((TextView) view.findViewById(R.id.tvTab)).setTextColor(this.getResources().getColor(R.color.multiStateButtonDisabled));
                        if (null != tabsNormalImg && tabsNormalImg.size() > i) {
                            iv.setImageResource(tabsNormalImg.get(i));
                        }
                    }
                }
            }

        }
    }

    public View.OnClickListener getCenterClickListener() {
        return centerClickListener;
    }

    public void setCenterClickListener(View.OnClickListener centerClickListener) {
        this.centerClickListener = centerClickListener;
    }
}
